package com.cskaoyan.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cglib.proxy.Enhancer;

import javax.sql.DataSource;

public class CustomBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("6、BeanPostProcessor的before");
        //也可以对这个bean做一些加工然后返回去
        //狸猫换太子 👉 bean → proxy
        if (beanName.contains("Service")) {
            //Object proxy = Enhancer.create();
            //return proxy;
        }
        if (bean instanceof DataSource) {
            //getPassword 654321
            //password 123456
            //setPassword 123456
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("9、BeanPostProcessor的after");
        return null;//return bean;//相当于没有处理直接返回去
    }
}
